var quizApp = angular.module("quizApp", ['ngAnimate', 'ngResource']);

quizApp.config(['$resourceProvider', function($resourceProvider){
    $resourceProvider.defaults.stripTrailingSlashes = false;
}]);

quizApp.controller('foxController', ['$scope', '$resource', function($scope, $resource){
    var q = 0;

    $scope.answerId = Infinity;
    $scope.selectedAnswers = [];
    $scope.checked = true;
    $scope.emptyFiled = true;
    $scope.s = 0;
    $scope.nope = false;
    //$scope.tmp;
    $scope.quiz = {};
    $scope.quiz.questions = ["Jak masz na imie?"];
    $scope.quiz.answers = [[]];

    (function(){
        var getJson = $resource('http://localhost:3000/q/548e0c81fe0d41941bcf35b4',
            {},
            { getJsonObject: {method: 'JSONP', isArray: false}}
        );

        getJson.get({}, function( data ){
            for ( var i = 0; i < data.questions.length; i++ ){
                $scope.quiz.questions.push( data.questions[i].content );
                var tmp = [];

                for ( var t = 0; t < data.questions[i].answers.length; t++ ){
                    tmp.push( data.questions[i].answers[t].content );
                }

                $scope.quiz.answers.push( tmp );
            }
        });
    })();


    this.getQuestion = function() {
        return $scope.quiz.questions[q];
    };

    this.getAnswers = function(){
        return $scope.quiz.answers[q];
    };


    this.next = function(){
        if ( q < $scope.quiz.questions.length - 1)
            q += 1;
        this.setAnswer(q);
    };

    this.prev = function(){
        if ( q != 0 )
            q -= 1;
        this.setAnswer(q);
    };

    this.currentQuestion = function() {
       return q;
    };

    this.questionLastIndexOf = function() {
        return $scope.quiz.questions.length - 1;
    };

    this.getAnswers = function(){
        return $scope.quiz.answers[q] || [];
    };

    this.selectAnswer = function ( answer ){
        this.saveAnswer( this.currentQuestion(), this.getIdAnswer(answer) );
        this.next();
    };

    this.getIdAnswer = function( answer ){
        for( var i = 0; i < $scope.quiz.answers[q].length; i++ ){
            if( $scope.quiz.answers[q][i] == answer ){
                return i;
            }
        }
    };

    this.isSelected = function ( answer ){
        return $scope.selectedAnswers[q] == this.getIdAnswer(answer);
    };

    //// zapisywanie dopoweidzi
    this.saveAnswer = function( questionId, answerId ){
        if ( $scope.selectedAnswers[questionId] == 'undefined' ) {
            $scope.selectedAnswers[questionId] = answerId;
            $scope.s += 1;
        } else if ( $scope.selectedAnswers[questionId] != 'undefined' ){
            $scope.selectedAnswers[questionId] = answerId;
        }
    };

    // ustaawianie wybranej odpowiedzi
    this.setAnswer = function( question ){
        var q = this.getIdAnswer( question )
        if ( $scope.selectedAnswers[q] != 'undefined' ) {
            this.isSelected( $scope.selectedAnswers[q] );
        }
    };
    // sprawdzanie indeksow funkcji == 'undefined' zeby uzyskac prawdziwa ilosc wybranych pytan
    Array.prototype.trueLength = function(){
        var trueLength = 0;

        for( var i = 0; i < this.length; i++ ){
            if( this[i] != undefined ){
                trueLength += 1;
            }
        }
        return trueLength;
    };

    this.allAnswers = function(){
        return $scope.selectedAnswers.trueLength() == $scope.quiz.questions.length - 1;
    };

    this.itsOk = function(){
        return $scope.checked;
    };

    this.check = function(){
        if ( !this.allAnswers() ) {
            $scope.checked = false;
        } else {
            $scope.checked = true;

            for ( var i = 1; i <= $scope.selectedAnswers.trueLength(); i++ ){
                console.log( "Pytanie: " + i + " odpowiedź: " + $scope.selectedAnswers[i] );
            }
        }
    };
}]);


quizApp.controller('quizzesController', ['$scope', '$resource', function($scope, $resource){
    $scope.quizzes_list = [];
    (function(){
        var getJson = $resource('http://localhost:3000/q',
            {},
            { get: {method: 'GET', isArray: true}}
        );

        getJson.get({}, function( data ){
            for ( var i = 0; i < data[0].questions.length; i++ ){
                $scope.quizzes_list.push( data[0].questions[i] );
            }
        });
    })();

    this.open_quiz = function( id ){
        window.open( "http://localhost:3000/q/" + id )
    };

    this.list_quizzes = function(){
        return $scope.quizzes_list;
    }
}]);